//
//  JQFXHLoginViewController.swift
//  jiuzhoudai
//
//  Created by 卢小艳 on 2025/7/9.
//

import Foundation
import UIKit
import SnapKit

class JQFXHLoginViewController: HJYJBaseViewController {
    var type = 1
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.updateServiceInfo()
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.jqfxhIsNavBarjjHidden = true
        self.view.addSubview(jqfxhViewxxBgImg)
        self.view.addSubview(jqfxhBackyyBtn)
        self.view.addSubview(jqfxhLogBackzzView)

        jqfxhViewxxBgImg.snp.makeConstraints { make in
            make.edges.equalTo(self.view)
        }
        
        jqfxhBackyyBtn.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(20)
            make.top.equalTo(self.view).offset(UIApplication.shared.jqfxhSafeAreaInsets.top + 20)
            make.width.height.equalTo(30)
        }
        
        
        jqfxhLogBackzzView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.jqfxhBackyyBtn.snp.bottom).offset(80)
            make.bottom.equalTo(self.view)
        }
        
        jqfxhLogBackzzView.addSubview(jqfxhPhoneaaTitleLabel)
        jqfxhPhoneaaTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(jqfxhLogBackzzView).offset(39)
            make.top.equalTo(jqfxhLogBackzzView).offset(39)
        }

        
        jqfxhLogBackzzView.addSubview(jqfxhTelbbbg)
        jqfxhTelbbbg.snp.makeConstraints { make in
            make.left.equalTo(jqfxhPhoneaaTitleLabel)
            make.centerX.equalTo(jqfxhLogBackzzView)
            make.top.equalTo(jqfxhPhoneaaTitleLabel.snp.bottom).offset(13)
            make.height.equalTo(41)
        }
        
        jqfxhTelbbbg.addSubview(jqfxhTelffTf)
        jqfxhTelffTf.snp.makeConstraints { make in
            make.left.equalTo(jqfxhTelbbbg).offset(19)
            make.right.equalTo(jqfxhTelbbbg).offset(-15)
            make.top.bottom.equalTo(jqfxhTelbbbg)
        }
        
        jqfxhLogBackzzView.addSubview(jqfxhCodeTitleggLabel)
        jqfxhCodeTitleggLabel.snp.makeConstraints { make in
            make.left.equalTo(jqfxhPhoneaaTitleLabel)
            make.top.equalTo(jqfxhTelbbbg.snp.bottom).offset(39)
        }
        
        jqfxhLogBackzzView.addSubview(jqfxhCodeccBg)
        jqfxhCodeccBg.snp.makeConstraints { make in
            make.left.right.height.equalTo(jqfxhTelbbbg)
            make.top.equalTo(jqfxhCodeTitleggLabel.snp.bottom).offset(13)
        }
            
        jqfxhCodeccBg.addSubview(jqfxhCodehhTf)
        jqfxhCodehhTf.snp.makeConstraints { make in
            make.left.equalTo(jqfxhCodeccBg).offset(19)
            make.right.equalTo(jqfxhCodeccBg).offset(-15)
            make.top.bottom.equalTo(jqfxhCodeccBg)

        }
        
        jqfxhLogBackzzView.addSubview(jqfxhGetCodeiiBtn)
        jqfxhGetCodeiiBtn.snp.makeConstraints { make in
            make.top.bottom.equalTo(jqfxhCodeccBg)
            make.right.equalTo(jqfxhCodeccBg).offset(-15)
        }
        
        self.view.addSubview(jqfxhCodeLoginjjBtn)
        jqfxhCodeLoginjjBtn.snp.makeConstraints { make in
            make.right.equalTo(jqfxhCodehhTf)
            make.top.equalTo(jqfxhCodehhTf.snp.bottom).offset(10)
        }
        
        
        self.view.addSubview(jqfxhAgreellButton)
        jqfxhAgreellButton.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(jqfxhCodeccBg.snp.bottom).offset(50)
        }
        self.view.addSubview(jqfxhLogmmTitle)
        jqfxhLogmmTitle.snp.makeConstraints { make in
            make.centerY.equalTo(jqfxhAgreellButton)
            make.left.equalTo(jqfxhAgreellButton.snp.right).offset(1)
        }
        
        self.view.addSubview(jqfxhZhunnceBtn)
        jqfxhZhunnceBtn.snp.makeConstraints { make in
            make.centerY.equalTo(jqfxhAgreellButton)
            make.left.equalTo(jqfxhLogmmTitle.snp.right)
        }
        
        self.view.addSubview(jqfxhYinoosiBtn)
        self.view.addSubview(jqfxhLoginkkButton)

        jqfxhYinoosiBtn.snp.makeConstraints { make in
            make.centerY.equalTo(jqfxhAgreellButton)
            make.left.equalTo(jqfxhZhunnceBtn.snp.right)
        }
        
        
        jqfxhLoginkkButton.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhTelbbbg)
            make.top.equalTo(jqfxhCodeccBg.snp.bottom).offset(100)
            make.height.equalTo(41)
        }
        
        jqfxhGetCodeiiBtn.addTarget(self, action: #selector(jqfxhSendkkSMSCode), for: .touchUpInside)
        jqfxhLoginkkButton.addTarget(self, action: #selector(jqfxhCheckiiMobile), for: .touchUpInside)

    }
    
    
    
    @objc func updateServiceInfo() {
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhServicehhInfo, type:JQFXHServerddInfoData.self) { result in
            switch result {
            case.success(let serverInfo):
                
            JQFXHNetworkManager.shared.currentServerInfo = serverInfo
                
            case .failure(let error):
                print(error)
            }
        }
    }
    
    @objc func jqfxhSendkkSMSCode() {
        var mobileStr = ""
        if let mobile = jqfxhTelffTf.text {
            if mobile.count == 0{
                JQFXHHUD.jqfxhShowwwError("请输入手机号")
                return
            }else {
                if !self.isValidPhoneNumber(mobile) {
                    JQFXHHUD.jqfxhShowwwError("请输入正确的手机号")
                    return
                }
                mobileStr = mobile
            }
        }else {
            JQFXHHUD.jqfxhShowwwError("请输入手机号")
            return
        }
        JQFXHHUD.jqfxhShowssLoading()

        if JQFXHNetworkManager.shared.partnerId == 6 {
            let provider = JQFXHNetworkManager.shared.moliProvider
            JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhMoliSendggMsg(mobile: mobileStr)) { result in
                JQFXHHUD.jqfxhHttidden()
                switch result {
                case.success(_):
                    self.jqfxhGetCodeiiBtnStartCountDown()
                case .failure(let error):
                    print(error)
                }
            }
            
            
        }else if JQFXHNetworkManager.shared.partnerId == 3 {
            let provider = JQFXHNetworkManager.shared.shengrProvider
            JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhShengrwwSendMsg(mobile: mobileStr)) { result in
                JQFXHHUD.jqfxhHttidden()
                switch result {
                case.success(_):
                    self.jqfxhGetCodeiiBtnStartCountDown()
                case .failure(let error):
                    print(error)
                }
            }
            
            
        }else {
            let provider = JQFXHNetworkManager.shared.provider
            JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhSendkkSMS(mobile: mobileStr)) { result in
                JQFXHHUD.jqfxhHttidden()
                switch result {
                case.success(_):
                    self.jqfxhGetCodeiiBtnStartCountDown()
                case .failure(let error):
                    print(error)
                }
            }
        }
        
    }
    
    func jqfxhGetCodeiiBtnStartCountDown() -> Void {
        self.jqfxhGetCodeiiBtn.jqfxhStartCountlldown()

    }
    
    private func isValidPhoneNumber(_ phone: String) -> Bool {
        let phoneRegex = "^1[3-9]\\d{9}$"
        let phonePredicate = NSPredicate(format: "SELF MATCHES %@", phoneRegex)
        return phonePredicate.evaluate(with: phone)
    }
    
    
   
    
    
   
    
    @objc func login() {
        
        if !self.jqfxhAgreellButton.isSelected {
            JQFXHHUD.jqfxhShowwwError("请同意相关协议")
            return
        }
        var mobileStr = ""
        var codeStr = ""
        if let mobile = jqfxhTelffTf.text {
            if mobile.count == 0 {
                JQFXHHUD.jqfxhShowwwError("请输入手机号")
                return
            }else {
                if !self.isValidPhoneNumber(mobile) {
                    JQFXHHUD.jqfxhShowwwError("请输入正确的手机号")
                    return
                }
                mobileStr = mobile
            }
        }else {
            JQFXHHUD.jqfxhShowwwError("请输入手机号")
            return
        }
        if let code = jqfxhCodehhTf.text {
            if code.count == 0 {
                JQFXHHUD.jqfxhShowwwError(type == 1 ? "请输入验证码" : "请输入密码")
                return
            }else {
                codeStr = code
            }
        }else {
            JQFXHHUD.jqfxhShowwwError(type == 1 ? "请输入验证码" : "请输入密码")
            return
        }
        JQFXHHUD.jqfxhShowssLoading()
        
        if self.type == 1 {
            if JQFXHNetworkManager.shared.partnerId == 6 {
                self.jqfxhMoliuuLogin(mobileStr: mobileStr, codeStr: codeStr)
            }else if JQFXHNetworkManager.shared.partnerId == 3 {
                self.jqfxhShengrtuLogin(mobileStr: mobileStr, codeStr: codeStr)
            }else {
                self.jqfxhBenbuvvgin(mobileStr: mobileStr, codeStr: codeStr)
            }
        }else {
            self.jqfxhBenbuvvgin(mobileStr: mobileStr, codeStr: codeStr)
        }

        
    }
    
    @objc func jqfxhCheckiiMobile() {
        var mobileStr = ""
        if let mobile = jqfxhTelffTf.text {
            if mobile.count == 0{
                JQFXHHUD.jqfxhShowwwError("请输入手机号")
                return
            }else {
                if !self.isValidPhoneNumber(mobile) {
                    JQFXHHUD.jqfxhShowwwError("请输入正确的手机号")
                    return
                }
                mobileStr = mobile
            }
        }else {
            JQFXHHUD.jqfxhShowwwError("请输入手机号")
            return
        }
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhCheckiiMobile(mobile: mobileStr),type: JQFXHMobileeeInfo.self) { result in
            switch result {
            case.success(let mobile):
                JQFXHNetworkManager.shared.mobileType = mobile.type
                if mobile.type == 1 {
                    JQFXHHUD.jqfxhShowxxText("账号已注销")
                    return
                }else if mobile.type == 0 {
                    self.login()
                }else if mobile.type == 2 {
                    JQFXHNetworkManager.shared.currentServerInfo?.app_config.partner_id = 1
                    self.login()
                }else if mobile.type == 3 {
                    self.login()
                }
            case .failure(let error):
                print(error)
            }
        }
    }
    
    func jqfxhShengrtuLogin(mobileStr: String,codeStr: String) -> Void {
        let provider = JQFXHNetworkManager.shared.shengrProvider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhShengrxxLogin(mobile: mobileStr, code: codeStr), type: User.self) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(let user):
                print(user)
                var userV = user
                userV.mobile = mobileStr
                JQFXHNetworkManager.shared.jqfxhCurrentUser = userV
                self.jqfxhReporoot()
            case .failure(let error):
                print(error)
            }
        }
    }
    
    
    
    func jqfxhBenbuvvgin(mobileStr: String,codeStr: String) -> Void {
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhLllogin(mobile: mobileStr, code: codeStr, password: codeStr, type: self.type), type: User.self) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(let user):
                print(user)
                var userV = user
                userV.mobile = mobileStr
                JQFXHNetworkManager.shared.jqfxhCurrentUser = userV
                if self.type == 2 {
                    JQFXHNetworkManager.shared.currentServerInfo?.app_config.partner_id = 1
                }
                self.navigationController?.popViewController(animated: true)
            case .failure(let error):
                print(error)
            }
        }
    }
    
    func jqfxhMoliuuLogin(mobileStr: String,codeStr: String) -> Void {
        let provider = JQFXHNetworkManager.shared.moliProvider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhMolirrLogin(mobile: mobileStr, code: codeStr), type: User.self) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(let user):
                print(user)
                var userV = user
                userV.mobile = mobileStr
                JQFXHNetworkManager.shared.jqfxhCurrentUser = userV
                self.jqfxhReporoot()
            case .failure(let error):
                print(error)
            }
        }
    }
    
    
    @objc func jqfxhAgreellButtonClick() {
        jqfxhAgreellButton.isSelected = !jqfxhAgreellButton.isSelected
    }
    
    @objc func jqfxhDonewwTapped() {
        view.endEditing(true) // 收起键盘
    }
    
    @objc func jqfxhBackyyBtnClick() {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    func jqfxhReporoot() -> Void {
        JQFXHHUD.jqfxhShowssLoading()
        let provider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhReporoot(loginStatus: "1", formStatus: "")) { result in
            JQFXHHUD.jqfxhHttidden()
            self.navigationController?.popViewController(animated: true)

        }
    }
    

    @objc func secrectLoginBtnClick() {
        
        if self.type == 1 {
            self.jqfxhGetCodeiiBtn.isHidden = true
            self.jqfxhCodehhTf.keyboardType = .default
            self.jqfxhCodehhTf.isSecureTextEntry = true
            self.jqfxhCodehhTf.placeholder = "请输入密码"
            self.jqfxhCodeTitleggLabel.text = "密码"
            self.type = 2
            self.jqfxhCodehhTf.text = ""
            jqfxhCodeLoginjjBtn.setTitle("验证码登录", for: .normal)

        }else {
            self.jqfxhGetCodeiiBtn.isHidden = false
            self.jqfxhCodehhTf.keyboardType = .numberPad
            self.jqfxhCodehhTf.isSecureTextEntry = false
            self.jqfxhCodehhTf.placeholder = "请输入验证码"
            self.jqfxhCodeTitleggLabel.text = "验证码"
            self.type = 1
            self.jqfxhCodehhTf.text = ""
            jqfxhCodeLoginjjBtn.setTitle("密码登录", for: .normal)

        }
        
    }
   
    
    
    
    lazy var jqfxhCodeccBg : UIView = {
        let jqfxhCodeccBg = UIView()
        jqfxhCodeccBg.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        jqfxhCodeccBg.layer.cornerRadius = 20
        jqfxhCodeccBg.layer.masksToBounds = true
        return jqfxhCodeccBg
    }()
    
    lazy var jqfxhViewxxBgImg : UIImageView = {
        let jqfxhViewxxBgImg = UIImageView(image: UIImage(named: "jqfxh_home_top"))
        jqfxhViewxxBgImg.isUserInteractionEnabled = true
        return jqfxhViewxxBgImg
    }()
    
    lazy var jqfxhBackyyBtn: UIButton = {
        let jqfxhBackyyBtn = UIButton(type: .custom)
        jqfxhBackyyBtn.setBackgroundImage(UIImage(named: "jqfxh_nav_white_back"), for: .normal)
        jqfxhBackyyBtn.addTarget(self, action: #selector(jqfxhBackyyBtnClick), for: .touchUpInside)
        return jqfxhBackyyBtn
    }()
    
    lazy var jqfxhTelbbbg : UIView = {
        let jqfxhTelbbbg = UIView()
        jqfxhTelbbbg.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        jqfxhTelbbbg.layer.cornerRadius = 20
        jqfxhTelbbbg.layer.masksToBounds = true
        return jqfxhTelbbbg
    }()
    
    lazy var jqfxhNameddImg : UIImageView = {
        let jqfxhViewxxBgImg = UIImageView(image: UIImage(named: "jqfxh_app_name"))
        return jqfxhViewxxBgImg
    }()
    
    lazy var jqfxhNameSubeeLabel : UILabel = {
        let jqfxhNameSubeeLabel = UILabel()
        jqfxhNameSubeeLabel.font = UIFont.jqfxhRegular14
        jqfxhNameSubeeLabel.textColor = UIColor.white
        jqfxhNameSubeeLabel.text = "极速到账，轻松借款"
        return jqfxhNameSubeeLabel
    }()
    

   
    
    
    lazy var jqfxhPhoneaaTitleLabel : UILabel = {
        let jqfxhPhoneaaTitleLabel = UILabel()
        jqfxhPhoneaaTitleLabel.font = UIFont.jqfxhMedium16
        jqfxhPhoneaaTitleLabel.textColor = UIColor.black
        jqfxhPhoneaaTitleLabel.text = "手机号"
        return jqfxhPhoneaaTitleLabel
    }()
    
    
    lazy var jqfxhLogBackzzView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = 32
        view.layer.masksToBounds = true
        return view
    }()
    
    lazy var jqfxhCodeTitleggLabel : UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = UIFont.jqfxhMedium16
        titleLabel.textColor = UIColor.black
        titleLabel.text = "验证码"
        return titleLabel
    }()
    
    lazy var jqfxhTelffTf : UITextField = {
        let jqfxhTelffTf = UITextField()
        jqfxhTelffTf.font = UIFont.jqfxhRegular14
        jqfxhTelffTf.keyboardType = .numberPad
        jqfxhTelffTf.placeholder = "请输入手机号"
        jqfxhTelffTf.jqfxhAddDoneButtoneeOnKeyboard(target: self, action: #selector(jqfxhDonewwTapped))
        return jqfxhTelffTf
    }()
    
    
   
    
    lazy var jqfxhCodehhTf : UITextField = {
        let jqfxhCodehhTf = UITextField()
        jqfxhCodehhTf.backgroundColor = UIColor.jqfxhSmallViewBgbbGray
        jqfxhCodehhTf.font = UIFont.jqfxhRegular14
        jqfxhCodehhTf.keyboardType = .numberPad
        jqfxhCodehhTf.placeholder = "请输入验证码"
        jqfxhCodehhTf.jqfxhAddDoneButtoneeOnKeyboard(target: self, action: #selector(jqfxhDonewwTapped))
        return jqfxhCodehhTf
    }()
    
    lazy var jqfxhCodeLoginjjBtn: UIButton = {
        let jqfxhCodeLoginjjBtn = UIButton(type: .custom)
        jqfxhCodeLoginjjBtn.setTitle("密码登录", for: .normal)
        jqfxhCodeLoginjjBtn.titleLabel?.font = UIFont.jqfxhRegular14
        jqfxhCodeLoginjjBtn.setTitleColor(.jqfxhThemexxRed, for: .normal)
        jqfxhCodeLoginjjBtn.addTarget(self, action: #selector(secrectLoginBtnClick), for: .touchUpInside)
        return jqfxhCodeLoginjjBtn
    }()
    
    
    lazy var jqfxhGetCodeiiBtn : JQFXHMsgeeCodeBtn = {
        let jqfxhGetCodeiiBtn = JQFXHMsgeeCodeBtn(type: .system)
        jqfxhGetCodeiiBtn.setTitle("发送验证码", for: .normal)
        jqfxhGetCodeiiBtn.setTitleColor(UIColor.jqfxhThemexxRed, for: .normal)
        jqfxhGetCodeiiBtn.titleLabel?.font = UIFont.jqfxhRegular12
        return jqfxhGetCodeiiBtn
    }()
    
   
    lazy var jqfxhLoginkkButton: UIButton = {
        let jqfxhLoginkkButton = UIButton(type: .system)
        jqfxhLoginkkButton.setTitle("登录", for: .normal)
        jqfxhLoginkkButton.titleLabel?.font = UIFont.jqfxhRegular16
        jqfxhLoginkkButton.backgroundColor = UIColor.jqfxhMaineeRed
        jqfxhLoginkkButton.setTitleColor(.white, for: .normal)
        jqfxhLoginkkButton.layer.cornerRadius = 8
        return jqfxhLoginkkButton
    }()
    
    
    lazy var jqfxhLogmmTitle : UILabel = {
        let jqfxhLogmmTitle = UILabel()
        //登录即代表同意《注册协议》《隐私协议》
        jqfxhLogmmTitle.font = UIFont.jqfxhRegular12
        jqfxhLogmmTitle.textColor = UIColor.black
        jqfxhLogmmTitle.text = "登录即代表同意"
        return jqfxhLogmmTitle
    }()
    
    lazy var jqfxhZhunnceBtn : UIButton = {
        let jqfxhZhunnceBtn = UIButton(type: .custom)
        jqfxhZhunnceBtn.setTitle("《注册协议》", for: .normal)
        jqfxhZhunnceBtn.setTitleColor(UIColor.jqfxhThemexxRed, for: .normal)
        jqfxhZhunnceBtn.titleLabel?.font = UIFont.jqfxhRegular12
        jqfxhZhunnceBtn.addTarget(self, action: #selector(jqfxhZhunnpqceBtnClick), for: .touchUpInside)
        return jqfxhZhunnceBtn
    }()
    
    lazy var jqfxhAgreellButton: UIButton = {
        let jqfxhAgreellButton = UIButton(type: .custom)
        jqfxhAgreellButton.setImage(UIImage(named: "jqfxh_no_agree"), for: .normal)
        jqfxhAgreellButton.setImage(UIImage(named: "jqfxh_agree"), for: .selected)
        jqfxhAgreellButton.backgroundColor = UIColor.white
        var configuration = UIButton.Configuration.plain()
        configuration.contentInsets = NSDirectionalEdgeInsets(top: 2, leading: 10, bottom: 2, trailing: 5)
        jqfxhAgreellButton.configuration = configuration
        jqfxhAgreellButton.tintColor = .clear  // 去掉选中时的蓝色背景
        jqfxhAgreellButton.addTarget(self, action: #selector(jqfxhAgreellButtonClick), for: .touchUpInside)
        return jqfxhAgreellButton
    }()
    
    
    
    lazy var jqfxhYinoosiBtn : UIButton = {
        let jqfxhYinoosiBtn = UIButton(type: .custom)
        jqfxhYinoosiBtn.setTitle("《隐私协议》", for: .normal)
        jqfxhYinoosiBtn.setTitleColor(UIColor.jqfxhThemexxRed, for: .normal)
        jqfxhYinoosiBtn.titleLabel?.font = UIFont.jqfxhRegular12
        jqfxhYinoosiBtn.addTarget(self, action: #selector(jqfxhYisirsClick), for: .touchUpInside)

        return jqfxhYinoosiBtn
    }()
   
    @objc func jqfxhYisirsClick() -> Void {
        let web = JQFXHWebViewController.init(jqfxhUrlccString: JQFXHCompanyMessage.jqfxhPrivacyPolicyURL, jqfxhTitle: "隐私协议")
        self.present(web, animated: true)
    }
    
    
    @objc func jqfxhZhunnpqceBtnClick() -> Void {
        let web = JQFXHWebViewController.init(jqfxhUrlccString: JQFXHCompanyMessage.jqfxhUserAgreementURL, jqfxhTitle: "注册协议")
        self.present(web, animated: true)
    }
    
}
